﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class competition : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        btnSubmit.Click += new EventHandler(btnSubmit_Click);
    }

    void btnSubmit_Click(object sender, EventArgs e)
    {
        if (IsValid) {
            Chimes.DataContext context = new Chimes.DataContext();
            Chimes.Competition comp = new Chimes.Competition();

            comp.FirstName = txtFirstname.Text;
            comp.Surname = txtSurname.Text;
            comp.Email = txtEmail.Text;
            comp.Address1 = txtAddress1.Text;
            comp.Address2 = txtAddress2.Text;
            comp.PostCode = txtPostCode.Text;
            comp.Optout = chkOptout.Checked;
            
            context.Competitions.InsertOnSubmit(comp);
            context.SubmitChanges();

            Email email = new Email(comp.Email, Server.MapPath("~/email/Competition.xml"));
            email.Format("Name", comp.FirstName);
            email.Send();

            pnlThankyou.Visible = true;
            pnlEntry.Visible = false;
        }
    }
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        args.IsValid = chkTC.Checked; 
    }
}
